library(tidycensus)
library(dplyr)
library(mapview)
library(readr)
library(dplyr)
library(sf)
library(ggplot2)Introduction to Census data
Questions
- How do we access census data?
objectives
- Combine census data with iNaturalist data
Load packages
U.S. Census data
We are using tidycensus to access U.S. Census data.
iNaturalist data
Let’s get observations for the Great Blue Heron.
inat <- read_csv('data/cleaned/cnc-los-angeles-observations.csv')inat_obs <- inat %>%
st_as_sf(coords = c("longitude", "latitude"), crs = 4326, remove=FALSE) %>%
filter(common_name == 'Great Blue Heron') mapview(inat_obs)Median income by census tract
Let’s get the median income for every census track in Los Angeles County from the 2020 American Community Survey.
la_income <- get_acs(
geography = "tract",
variables = "B19013_001",
state = "CA",
county="Los Angeles",
year = 2020,
geometry = TRUE
)Getting data from the 2016-2020 5-year ACS
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
The U.S. Census provides geographic data using the NAD 1983 geographic coordinate system.
st_crs(la_income)Coordinate Reference System:
User input: NAD83
wkt:
GEOGCRS["NAD83",
DATUM["North American Datum 1983",
ELLIPSOID["GRS 1980",6378137,298.257222101,
LENGTHUNIT["metre",1]]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
CS[ellipsoidal,2],
AXIS["latitude",north,
ORDER[1],
ANGLEUNIT["degree",0.0174532925199433]],
AXIS["longitude",east,
ORDER[2],
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4269]]
We want to convert the CRS of he census data to match the CRS of the iNaturalist data.
la_income <- st_transform(la_income, crs = 4326)compare the CRS
st_crs(la_income) == st_crs(inat_obs)[1] TRUE
Use ggplot2 to create a static map.
ggplot() +
geom_sf(aes(fill = estimate), data=la_income) 
Add iNaturalist data.
ggplot() +
geom_sf(aes(fill = estimate), data=la_income) +
geom_sf(data=inat_obs)
Use mapview to create an interactive map.
mapview(la_income, zcol = "estimate")add iNaturalist dadta
mapview(la_income, zcol = "estimate") +
mapview(inat_obs)Population by census tract
Get population by census tract for Los Angeles County
la_pop <- get_decennial(
geography = "tract",
variables = c(population="P1_001N"),
state = "CA",
county = "Los Angeles",
year = 2020,
geometry = TRUE
)Getting data from the 2020 decennial Census
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using the PL 94-171 Redistricting Data Summary File
Note: 2020 decennial Census data use differential privacy, a technique that
introduces errors into data to preserve respondent confidentiality.
ℹ Small counts should be interpreted with caution.
ℹ See https://www.census.gov/library/fact-sheets/2021/protecting-the-confidentiality-of-the-2020-census-redistricting-data.html for additional guidance.
This message is displayed once per session.
We want to convert the CRS of he census data to match the CRS of the iNaturalist data.
la_pop <- st_transform(la_pop, crs = 4326)Create static map
ggplot() +
geom_sf(aes(fill = value), data=la_pop) +
geom_sf(data = inat_obs)
Create interactive map
mapview(la_pop, zcol="value") +
mapview(inat_obs)Ethnicity by census tract
Get the percentage for each ethnicity for each census tract.
la_ethnicity <- get_decennial(
geography = "tract",
state = "CA",
county = "Los Angeles",
variables = c(
Hispanic = "P2_002N",
White = "P2_005N",
Black = "P2_006N",
Native = "P2_007N",
Asian = "P2_008N"
),
summary_var = "P2_001N",
year = 2020,
geometry = TRUE
) %>%
mutate(percent = 100 * (value / summary_value)) Getting data from the 2020 decennial Census
Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using the PL 94-171 Redistricting Data Summary File
We want to convert the CRS of the census data to match the CRS of the iNaturalist data.
la_ethnicity <- st_transform(la_ethnicity, crs = 4326)Get the Hispanic data.
la_hispanic <- filter(la_ethnicity, variable == "Hispanic")Create static map
ggplot() +
geom_sf(aes(fill = percent), data=la_hispanic) +
geom_sf(data = inat_obs)
Create interactive map
mapview(la_hispanic, zcol='percent') +
mapview(inat_obs)